package com.mojiayi.learn.common;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class RemoveItemFromList {
    @Test
    public void testRemoveViaIterator() {
        List<Homework> homeworkList = new ArrayList<>();
        homeworkList.add(new Homework(1L, 123L));
        homeworkList.add(new Homework(2L, 234L));
        homeworkList.add(new Homework(3L, 345L));
        homeworkList.add(null);

        Iterator<Homework> iterator = homeworkList.iterator();
        while (iterator.hasNext()) {
            Homework homework = iterator.next();
            if (homework != null && homework.getScore() == 1) {
                iterator.remove();
            }
        }

        for (Homework homework : homeworkList) {
            System.out.println("score=" + homework.getScore() + ",studentUid=" + homework.getStudentUid());
        }
    }

    @Test
    public void testRemoveViaCollectionRemoveIf() {
        List<Homework> homeworkList = new ArrayList<>();
        homeworkList.add(new Homework(1L, 123L));
        homeworkList.add(new Homework(2L, 234L));
        homeworkList.add(new Homework(3L, 345L));
        homeworkList.add(null);

        homeworkList.removeIf(v -> v.getScore() == 1);

        for (Homework homework : homeworkList) {
            System.out.println("score=" + homework.getScore() + ",studentUid=" + homework.getStudentUid());
        }
    }
}

class Homework {
    private Long score;
    private Long studentUid;

    public Homework(Long score, Long studentUid) {
        this.score = score;
        this.studentUid = studentUid;
    }

    public Long getScore() {
        return score;
    }

    public void setScore(Long score) {
        this.score = score;
    }

    public Long getStudentUid() {
        return studentUid;
    }

    public void setStudentUid(Long studentUid) {
        this.studentUid = studentUid;
    }
}